﻿using System;
using System.Net.NetworkInformation;

namespace SnarlGMailNotifier.Forms
{
    class NetworkDetection
    {
        private static NetworkAddressChangedEventHandler addressChangeHandler;
        private static NetworkAvailabilityChangedEventHandler netAvailHandler;
        //private static PingCompletedEventHandler pingHandler;
        private static bool networkIsUp = false;
        private static bool wakeNotifierUp = false;
        private static FNotify objToWakeUp = null;

        static NetworkDetection()
        {
            addressChangeHandler = new NetworkAddressChangedEventHandler(NetworkChange_NetworkAddressChanged);
            netAvailHandler = new NetworkAvailabilityChangedEventHandler(NetworkChange_NetworkAvailabilityChanged);

            NetworkChange.NetworkAddressChanged += addressChangeHandler;
            NetworkChange.NetworkAvailabilityChanged += netAvailHandler;

            //pingHandler = new PingCompletedEventHandler(p_PingCompleted);
        }

        static void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
        {
            UpdateNetworkStatus();
        }

        static void NetworkChange_NetworkAddressChanged(object sender, EventArgs e)
        {
            UpdateNetworkStatus();
        }

        public static void UpdateNetworkStatus()
        {
            // FIX: internet ping check not working reliably
            //if (NetworkInterface.GetIsNetworkAvailable())
            //{
            //    // Just because network is available doesn't mean we're on the internet
            //    CheckHostReachable("www.google.com");
            //}
            bool oldState = networkIsUp;
            networkIsUp = NetworkInterface.GetIsNetworkAvailable();
            if (wakeNotifierUp && (oldState != networkIsUp))
            {
                objToWakeUp.wakeMeUp();
                wakeNotifierUp = false;
            }
        }

        //private  void CheckHostReachable(string host)
        //{
        //    try
        //    {
        //        System.Net.NetworkInformation.Ping ping = new Ping();
        //        ping.PingCompleted += pingHandler;
        //        ping.SendAsync(host, 20000, null);
        //    }
        //    catch
        //    {
        //        networkIsUp = false;
        //    }
        //}

        // void p_PingCompleted(object sender, PingCompletedEventArgs e)
        //{
        //    networkIsUp = (e.Error == null && e.Reply.Status == IPStatus.Success) ? true : false;
        //    System.Windows.Forms.MessageBox.Show("Network Status", networkIsUp.ToString());
        //}

        public static bool NetworkIsAvailable()
        {
            return networkIsUp;
        }

        public static bool wakeMeIfNetworkIsUpAgain(FNotify objToWake)
        {
            if (!wakeNotifierUp)
            {
                wakeNotifierUp = true;
                objToWakeUp = objToWake;
                return true;
            }
            else
            {
                return false;
            }

        }
    }
}
